/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DBConfig.java
 *
 * Created on 13-Mar-2011, 20:35:52
 */
package inventorymanagement;

import DataAccess.DBUtil;
import Entity.InfoDatabase;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.File;
import java.sql.Connection;
import javax.swing.JOptionPane;

/**
 *
 * @author dung
 */
public class DBConfig extends javax.swing.JDialog {

    InfoDatabase idb = null;
    Connection conn = null;

    /** Creates new form DBConfig */
    public DBConfig(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation(((int) screenSize.getWidth() - this.getWidth()) / 2, ((int) screenSize.getHeight() - this.getHeight()) / 2);
        File file = new File("config.properties");
        if (file.exists()) {
            idb = DBUtil.getInfoDB();
            if (idb != null) {
                txtDBServer.setText(idb.getServer());
                txtDBport.setText(idb.getPort());
                txtDBUser.setText(idb.getUser());
                txtDBpassword.setText(idb.getPass());
                txtDBName.setText(idb.getDatabase());
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtDBServer = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtDBport = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtDBUser = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtDBName = new javax.swing.JTextField();
        lblImageDB = new javax.swing.JLabel();
        txtDBpassword = new javax.swing.JPasswordField();
        btnDBTest = new javax.swing.JButton();
        btnDBOK = new javax.swing.JButton();
        btnDBCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 24));
        jLabel1.setText("Database Config");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Database Config"));

        jLabel2.setText("Server name");

        txtDBServer.setText("localhost");

        jLabel3.setText("Port");

        txtDBport.setText("1433");

        jLabel4.setText("Username");

        txtDBUser.setText("sa");

        jLabel5.setText("Password");

        jLabel6.setText("Database name");

        txtDBName.setText("inventory");

        txtDBpassword.setText("123456");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblImageDB, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtDBpassword)
                    .addComponent(txtDBName)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtDBServer, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtDBport, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(txtDBUser))
                .addContainerGap(35, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtDBServer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3)
                            .addComponent(txtDBport, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(txtDBUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(txtDBpassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(txtDBName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(lblImageDB, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE))
                .addContainerGap())
        );

        btnDBTest.setText("Test");
        btnDBTest.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDBTestActionPerformed(evt);
            }
        });

        btnDBOK.setText("OK");
        btnDBOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDBOKActionPerformed(evt);
            }
        });

        btnDBCancel.setText("Cancel");
        btnDBCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDBCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(177, 177, 177)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addComponent(btnDBTest, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(144, 144, 144)
                        .addComponent(btnDBOK)
                        .addGap(43, 43, 43)
                        .addComponent(btnDBCancel)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDBTest)
                    .addComponent(btnDBOK)
                    .addComponent(btnDBCancel))
                .addContainerGap(18, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnDBCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDBCancelActionPerformed
        // TODO add your handling code here:
        idb = new InfoDatabase(txtDBServer.getText(), txtDBport.getText(),
                txtDBName.getText(), txtDBUser.getText(),
                txtDBpassword.getText());
        conn = DBUtil.getConnection(idb);
        if (conn == null) {
            int confirm = JOptionPane.showConfirmDialog(this, "Are you quit?",
                    "Exit Databse Config", JOptionPane.OK_CANCEL_OPTION);
            if (confirm == JOptionPane.OK_OPTION) {
                System.exit(0);
            }
        } else {
            this.setVisible(false);
        }
    }//GEN-LAST:event_btnDBCancelActionPerformed

    private void btnDBTestActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDBTestActionPerformed
        // TODO add your handling code here:
        idb = new InfoDatabase(txtDBServer.getText(), txtDBport.getText(),
                txtDBName.getText(), txtDBUser.getText(), txtDBpassword.getText());
        conn = DBUtil.getConnection(idb);
        if (conn == null) {
            JOptionPane.showMessageDialog(null, "Cannot Connect Database!");
        } else {
            JOptionPane.showMessageDialog(null, "Connect Database successful");
        }
    }//GEN-LAST:event_btnDBTestActionPerformed

    private void btnDBOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDBOKActionPerformed
        // TODO add your handling code here
        if (txtDBServer.getText().equals("") || txtDBport.getText().equals("")
                || txtDBUser.getText().equals("") || txtDBpassword.getText().equals("")
                || txtDBName.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Fill the information!");
        } else {
            idb = new InfoDatabase(txtDBServer.getText(), txtDBport.getText(),
                    txtDBName.getText(), txtDBUser.getText(), txtDBpassword.getText());
            int reVal = JOptionPane.showConfirmDialog(this, "Store all information into the system", "Store information", JOptionPane.OK_CANCEL_OPTION);
            if (reVal == JOptionPane.OK_OPTION) {
                DBUtil.SetInfoDB(idb);
                conn = DBUtil.getConnection(idb);
                if (conn == null) {
                    JOptionPane.showMessageDialog(null, "Cannot connect database");
                } else {
                    this.setVisible(false);
                }
            }
        }
    }//GEN-LAST:event_btnDBOKActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDBCancel;
    private javax.swing.JButton btnDBOK;
    private javax.swing.JButton btnDBTest;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblImageDB;
    private javax.swing.JTextField txtDBName;
    private javax.swing.JTextField txtDBServer;
    private javax.swing.JTextField txtDBUser;
    private javax.swing.JPasswordField txtDBpassword;
    private javax.swing.JTextField txtDBport;
    // End of variables declaration//GEN-END:variables
}
